<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="企业的Id" prop="entId">
      <el-input v-model="dataForm.entId" placeholder="企业的Id"></el-input>
    </el-form-item>
    <el-form-item label="订单号" prop="orderCode">
      <el-input v-model="dataForm.orderCode" placeholder="订单号"></el-input>
    </el-form-item>
    <el-form-item label="订单状态" prop="orderStatus">
      <el-input v-model="dataForm.orderStatus" placeholder="订单状态"></el-input>
    </el-form-item>
    <el-form-item label="配送商品" prop="goodIds">
      <el-input v-model="dataForm.goodIds" placeholder="配送商品"></el-input>
    </el-form-item>
    <el-form-item label="配送数量" prop="number">
      <el-input v-model="dataForm.number" placeholder="配送数量"></el-input>
    </el-form-item>
    <el-form-item label="客户" prop="customerId">
      <el-input v-model="dataForm.customerId" placeholder="客户"></el-input>
    </el-form-item>
    <el-form-item label="0 未删 1已删" prop="delFlag">
      <el-input v-model="dataForm.delFlag" placeholder="0 未删 1已删"></el-input>
    </el-form-item>
    <el-form-item label="创建人" prop="createBy">
      <el-input v-model="dataForm.createBy" placeholder="创建人"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="更新人" prop="updateBy">
      <el-input v-model="dataForm.updateBy" placeholder="更新人"></el-input>
    </el-form-item>
    <el-form-item label="更新时间" prop="updateTime">
      <el-input v-model="dataForm.updateTime" placeholder="更新时间"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          entId: '',
          orderCode: '',
          orderStatus: '',
          goodIds: '',
          number: '',
          customerId: '',
          delFlag: '',
          createBy: '',
          createTime: '',
          updateBy: '',
          updateTime: ''
        },
        dataRule: {
          entId: [
            { required: true, message: '企业的Id不能为空', trigger: 'blur' }
          ],
          orderCode: [
            { required: true, message: '订单号不能为空', trigger: 'blur' }
          ],
          orderStatus: [
            { required: true, message: '订单状态不能为空', trigger: 'blur' }
          ],
          goodIds: [
            { required: true, message: '配送商品不能为空', trigger: 'blur' }
          ],
          number: [
            { required: true, message: '配送数量不能为空', trigger: 'blur' }
          ],
          customerId: [
            { required: true, message: '客户不能为空', trigger: 'blur' }
          ],
          delFlag: [
            { required: true, message: '0 未删 1已删不能为空', trigger: 'blur' }
          ],
          createBy: [
            { required: true, message: '创建人不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          updateBy: [
            { required: true, message: '更新人不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/app/entorder/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.entId = data.entOrder.entId
                this.dataForm.orderCode = data.entOrder.orderCode
                this.dataForm.orderStatus = data.entOrder.orderStatus
                this.dataForm.goodIds = data.entOrder.goodIds
                this.dataForm.number = data.entOrder.number
                this.dataForm.customerId = data.entOrder.customerId
                this.dataForm.delFlag = data.entOrder.delFlag
                this.dataForm.createBy = data.entOrder.createBy
                this.dataForm.createTime = data.entOrder.createTime
                this.dataForm.updateBy = data.entOrder.updateBy
                this.dataForm.updateTime = data.entOrder.updateTime
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/app/entorder/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'entId': this.dataForm.entId,
                'orderCode': this.dataForm.orderCode,
                'orderStatus': this.dataForm.orderStatus,
                'goodIds': this.dataForm.goodIds,
                'number': this.dataForm.number,
                'customerId': this.dataForm.customerId,
                'delFlag': this.dataForm.delFlag,
                'createBy': this.dataForm.createBy,
                'createTime': this.dataForm.createTime,
                'updateBy': this.dataForm.updateBy,
                'updateTime': this.dataForm.updateTime
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
